// 轮播图
$(function() {
	var uid = window.localStorage.getItem("user_id");
	// 主页轮播图
	$.ajax({
		url: 'http://www.teaweilai.com/app/Admin/Appbanner/BannerShow',
		type: 'POST',
		dataType: 'json',
		success: function(data) {
			// console.log(data);
			var strLi = template('carouselLi', {
				cal: data
			});
			$(".carousel-indicators").html(strLi);
			var strImg = template('carouselImg', {
				cal: data
			});
			$(".carousel-inner").html(strImg);
		}
	})

	// 四项主页
	$.ajax({
		url: 'http://www.teaweilai.com/app/Admin/AppSb/Sb',
		type: 'get',
		async: true,
		dataType: 'json',
		data: {},
		success: function(data) {
			var $htmlM = template('master', {
				master: data
			});
			$(".master>.row").html($htmlM);
		}
	});
	//茶券兑换coupon
	$.ajax({
		url: 'http://www.teaweilai.com/app/Admin/Appbanner/ConvertShow',
		type: 'get',
		async: true,
		dataType: 'json',
		success: function(data) {
			//console.log(data);
			var $htmlC = template('coupon', {
				coupon: data
			});
			$(".coupon>.row").html($htmlC);
		}
	});
	//推荐商品
	$.ajax({
		url: 'http://www.teaweilai.com/app/Admin/Appbanner/hotShop',
		type: 'get',
		async: true,
		dataType: 'json',
		success: function(data) {
			// console.log(data);
			var $htmlG = template('goods', {
				goods: data
			});
			$(".goodsAll").html($htmlG);
		}
	});

	// 茶券兑换
	$(document).on("click", "#coupon1", function(e) {
		e.preventDefault();
		$.ajax({
			url: 'http://www.teaweilai.com/app/Admin/Appbanner/EachShow',
			type: 'post',
			dataType: 'json',
			data: {
				face: "100"
			},
			success: function(data) {
				//console.log(data);
				window.sessionStorage.setItem("face", 100);
				window.location.href = "html/teaExchange.html";
			}
		})
	});
	$(document).on("click", "#coupon2", function(e) {
		e.preventDefault();
		$.ajax({
			url: 'http://www.teaweilai.com/app/Admin/Appbanner/EachShow',
			type: 'post',
			dataType: 'json',
			data: {
				face: "200"
			},
			success: function(data) {
				console.log(data);
				window.sessionStorage.setItem("face", 200);
				window.location.href = "html/teaExchange.html";
			}
		})
	});
	$(document).on("click", "#coupon3", function(e) {
		e.preventDefault();
		$.ajax({
			url: 'http://www.teaweilai.com/app/Admin/Appbanner/EachShow',
			type: 'post',
			dataType: 'json',
			data: {
				face: "500"
			},
			success: function(data) {
				console.log(data);
				window.sessionStorage.setItem("face", 500);
				window.location.href = "html/teaExchange.html";
			}
		})
	});
	$(document).on("click", "#coupon4", function(e) {
		e.preventDefault();
		$.ajax({
			url: 'http://www.teaweilai.com/app/Admin/Appbanner/EachShow',
			type: 'post',
			dataType: 'json',
			data: {
				face: "1000"
			},
			success: function(data) {
				console.log(data);
				window.sessionStorage.setItem("face", 1000);
				window.location.href = "html/teaExchange.html";
			}
		})
	});
	// 四个二级页面
	$(document).on("click", "#master22", function(e) {
		// 产地批发
		e.preventDefault();
		if (uid == null) {
			window.location.href = "html/login.html"
		} else {
			$.ajax({
					url: 'http://www.teaweilai.com/app/Admin/AppSb/Sb',
					type: 'POST',
					dataType: 'json',
					data: {
						uid: uid
					}
				})
				.done(function(data) {
					console.log(data);
					// 最开始版本
					// if (data.code == 500) {
					// 	window.location.href = "html/upload.html";
					// } else {
					// 	if (data.sing.is_check == 0) {
					// 		$(".mask").css({
					// 			display: 'block'
					// 		})
					// 		setTimeout(function() {
					// 			$(".mask").css({
					// 				display: 'none'
					// 			})
					// 		}, 2000)
					// 	} else {
					// 		window.location.href = "html/wholesale.html";
					// 	}
					// }
					// 5.17开会改版本
					// 300 正在审核  500上传  200 
					if (data.code == 500) {
						window.location.href = "html/upload.html";
					} else if (data.code == 200) {
						window.location.href = "html/wholesale.html";
					} else if (data.code == 300) {
						$(".mask").css({
							display: 'block'
						})
						setTimeout(function() {
							$(".mask").css({
								display: 'none'
							})
						}, 2000)
					}
				})
				.fail(function() {
					console.log("error");
				})
				.always(function() {
					console.log("complete");
				});

		}
	})
	$(document).on("click", "#master23", function(e) {
		e.preventDefault();
		window.location.href = "html/teaShop.html";
	})
	$(document).on("click", "#master24", function(e) {
		e.preventDefault();
		window.location.href = "html/oneForone.html";
	})
	$(document).on("click", "#master25", function(e) {
			e.preventDefault();
			window.location.href = "html/cityShop.html";
		})
		// 点击商品
	$(document).on("click", ".goods", function() {
			var gid = $(this).eq(0).attr('data-goods_id');
			console.log(gid);
			window.sessionStorage.setItem("gid", gid);
			window.location.href = "html/tea_detail.html";
		})
		// 搜索
	$(".searchBtn").click(function() {
		window.sessionStorage.setItem("val", $(".searchIpt").val());
		window.sessionStorage.setItem("type", $(".chose option:checked").attr("data-attr"));
		window.location.href = "html/search.html";
	})

	// 首页购物车数量
	if (uid == null) {
		$(".count").html("");
	} else {
		$.ajax({
			url: 'http://www.teaweilai.com/app/Admin/AppCart/cartList',
			type: 'POST',
			dataType: 'json',
			data: {
				uid: uid
			},
			success: function(data) {
				console.log(data);
				var countCar = 0;
				if (data.sing.goods == null) {
					$(".count").html('');
				} else {
					for (var i = 0; i < data.sing.goods.length; i++) {
						countCar += data.sing.goods[i].goodsList.length;
					}
					$(".count").html(countCar);
				}


			}
		})
	}
})